function fig = fm_uwfig(varargin)
% FM_UWFIG create GUI for PSAT/UWPFLOW interface.
%
% FIG = FM_UWFIG
%
%see UWPFLOW structure for settings
%
%Author:    Federico Milano
%Date:      31-Mar-2003
%Version:   1.0.0
%
%E-mail:    federico.milano@ucd.ie
%Web-site:  faraday1.ucd.ie/psat.html
%
% Copyright (C) 2002-2019 Federico Milano

global Settings Path PQ UWPFLOW GAMS Theme Fig Hdl History

% initialize UWPFLOW.opt, if necessary
fm_uwpflow('init')

% check for options
if nargin, checkon(varargin{1}), return, end

% do not redraw figure if open
if ishandle(Fig.uwpflow), figure(Fig.uwpflow), return, end

[u,w] = system('uwpflow');
if isempty(strmatch('UW Continuation Power Flow',w))
  uiwait(fm_choice('UWPFLOW is not properly installed on your system.',2))
  return
end

% constants and lists
D = 0.9394;
dy = 0.025;
dx = (D-4*dy)/3;
x1 = 0.0329 + dy;
x2 = 0.0329 + 2*dy + dx;
x3 = 0.0329 + 3*dy + 2*dx;

methods = {'[  ] Power Flow';
           '[-c] Continuation Method';
           '[-C] Direct Method';
           '[-H] Parameterized CM'};

output = {'.k';   '.v';   '.w';   '.pf'; '.jac'; '.cf'; '.cpf';
          '.mis'; '.var'; '.log'; '.oh'; '.vp'; '.gen'; '.ini';
          '.poc'; '.ntv'};
output = fm_strjoin(UWPFLOW.file,output);

if PQ.n
  PQbuses = fm_strjoin('PQ_',num2str(PQ.bus));
  if PQ.n < UWPFLOW.opt.B.num, UWPFLOW.opt.B.num = 1; end
  if PQ.n < UWPFLOW.opt.f.num, UWPFLOW.opt.f.num = 1; end
  if PQ.n < UWPFLOW.opt.one.num, UWPFLOW.opt.one.num = 1; end
else
  PQbuses = {'<none>'};
end

if strcmp(Settings.platform,'MAC')
  aligntxt = 'center';
  dm = 0.0075;
else
  aligntxt = 'left';
  dm = 0;
end

h0 = figure('Units','normalized', ...
            'Color',Theme.color02, ...
            'Colormap',[], ...
            'CreateFcn', 'Fig.uwpflow = gcf;', ...
            'DeleteFcn', 'Fig.uwpflow = -1;', ...
            'MenuBar','none', ...
            'Name','PSAT-UWPFLOW', ...
            'NumberTitle','off', ...
            'PaperPosition',[18 180 576 432], ...
            'PaperUnits','points', ...
            'Position',sizefig(1.2*0.5645,1.1*0.8451), ...
            'Resize','on', ...
            'ToolBar','none', ...
            'FileName','fm_uwfig');
fm_set colormap

% Menu File
h1 = uimenu('Parent',h0, ...
            'Label','File', ...
            'Tag','MenuFile');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwpflow view', ...
            'Label','View UWPFLOW Input/Output files', ...
            'Tag','OTV', ...
            'Accelerator','g');
h2 = uimenu('Parent',h1, ...
            'Callback','close(gcf)', ...
            'Label','Exit', ...
            'Tag','NetSett', ...
            'Accelerator','x', ...
            'Separator','on');

% Menu Edit
h1 = uimenu('Parent',h0, ...
            'Label','Edit', ...
            'Tag','MenuEdit');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwpflow makecom', ...
            'Label','Create UWPFLOW command line', ...
            'Tag','ToolUWcom', ...
            'Accelerator','c');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_setting', ...
            'Label','General Settings', ...
            'Tag','ToolSett', ...
            'Separator', 'on', ...
            'Accelerator','s');

% Menu Run
h1 = uimenu('Parent',h0, ...
            'Label','Run', ...
            'Tag','MenuRun');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwpflow uwrun', ...
            'Label','Run UWPFLOW', ...
            'Tag','ToolOPFSett', ...
            'Accelerator','z');

% Menu Options
h1 = uimenu('Parent',h0, ...
            'Label','Options', ...
            'Tag','MenuOpt');

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig a', ...
            'Label','[-a] No tap/angle limit control', ...
            'Tag','aopt', ...
            'Checked',onoff(UWPFLOW.opt.a.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig A', ...
            'Label','[-A] No intercahnge area control', ...
            'Tag','Aopt', ...
            'Checked',onoff(UWPFLOW.opt.A.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig b', ...
            'Label','[-b] No interchange area control', ...
            'Tag','bopt', ...
            'Checked',onoff(UWPFLOW.opt.b.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig d', ...
            'Label','[-d] Generate debug output', ...
            'Tag','dopt', ...
            'Checked',onoff(UWPFLOW.opt.d.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig g', ...
            'Label','[-g] Force Qg to 0 (IEEE CDF)', ...
            'Tag','gopt', ...
            'Checked',onoff(UWPFLOW.opt.g.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig G', ...
            'Label','[-G] Turn off ac device recovery', ...
            'Tag','Gopt', ...
            'Checked',onoff(UWPFLOW.opt.G.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig n', ...
            'Label','[-n] Turn off all ac limits', ...
            'Tag','nopt', ...
            'Checked',onoff(UWPFLOW.opt.n.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig N', ...
            'Label','[-N] Turn off all ac system controls', ...
            'Tag','Nopt', ...
            'Checked',onoff(UWPFLOW.opt.N.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig p', ...
            'Label','[-p] Turn off P/Q limits in reg. transf.', ...
            'Tag','popt', ...
            'Checked',onoff(UWPFLOW.opt.p.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig P', ...
            'Label','[-P] Turn off P/Q control by reg. transf.', ...
            'Tag','Popt', ...
            'Checked',onoff(UWPFLOW.opt.P.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig q', ...
            'Label','[-q] Turn off Q limits in PV buses', ...
            'Tag','qopt', ...
            'Checked',onoff(UWPFLOW.opt.q.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig qx', ...
            'Label','[-qx] Turn off V limits in BX buses', ...
            'Tag','qxopt', ...
            'Checked',onoff(UWPFLOW.opt.qx.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig qz', ...
            'Label','[-qz] Turn off Q limits in BZ buses', ...
            'Tag','qzopt', ...
            'Checked',onoff(UWPFLOW.opt.qz.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig Q', ...
            'Label','[-Q] Turn off remote Vg control', ...
            'Tag','Qopt', ...
            'Checked',onoff(UWPFLOW.opt.Q.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig QX', ...
            'Label','[-QX] Turn off remote Vg control in BX buses', ...
            'Tag','QXopt', ...
            'Checked',onoff(UWPFLOW.opt.QX.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig r', ...
            'Label','[-r] Turn off V limits in reg. tranf. and PV buses', ...
            'Tag','ropt', ...
            'Checked',onoff(UWPFLOW.opt.r.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig R', ...
            'Label','[-R] Turn off V control by reg. transf.', ...
            'Tag','Ropt', ...
            'Checked',onoff(UWPFLOW.opt.R.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig s', ...
            'Label','[-s] Suppress ASCII output file', ...
            'Tag','sopt', ...
            'Checked',onoff(UWPFLOW.opt.s.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig x', ...
            'Label','[-x] Use single slack bus', ...
            'Tag','xopt', ...
            'Checked',onoff(UWPFLOW.opt.x.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig X', ...
            'Label','[-X] Turn off max Pg limits', ...
            'Tag','Xopt', ...
            'Checked',onoff(UWPFLOW.opt.X.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig four', ...
            'Label','[-4] Turn off Eq limits in all gen.', ...
            'Tag','fouropt', ...
            'Checked',onoff(UWPFLOW.opt.four.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig five', ...
            'Label','[-5] Turn off Ia limits in all gen.', ...
            'Tag','fiveopt', ...
            'Checked',onoff(UWPFLOW.opt.five.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig seven', ...
            'Label','[-7] Enforce Vmax and Vmin', ...
            'Tag','sevenopt', ...
            'Checked',onoff(UWPFLOW.opt.seven.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig eight', ...
            'Label','[-8] Enforce Imax limits', ...
            'Tag','eightopt', ...
            'Checked',onoff(UWPFLOW.opt.eight.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig nine', ...
            'Label','[-9] Do not enforce gen. Smax limits', ...
            'Tag','nineopt', ...
            'Checked',onoff(UWPFLOW.opt.nine.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig bound', ...
            'Label','[-#] Use secondary voltage control', ...
            'Tag','boundopt', ...
            'Checked',onoff(UWPFLOW.opt.bound.status));

% Menu Output files

h1 = uimenu('Parent',h0, ...
            'Label','Output', ...
            'Tag','MenuOut');

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig E', ...
            'Label','[-E] Print PoC right e-vector', ...
            'Tag','eopt', ...
            'Checked',onoff(UWPFLOW.opt.E.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig j', ...
            'Label','[-j] Write Jacobian matrix (2n+1)x(2n+1)', ...
            'Tag','eopt', ...
            'Checked',onoff(UWPFLOW.opt.j.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig J', ...
            'Label','[-J] Write Jacobian matrix (2n)x(2n)', ...
            'Tag','eopt', ...
            'Checked',onoff(UWPFLOW.opt.j.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig E', ...
            'Label','[-E] Print PoC right e-vector', ...
            'Tag','eopt', ...
            'Checked',onoff(UWPFLOW.opt.E.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig l', ...
            'Label','[-l] Write standard error output', ...
            'Tag','lopt', ...
            'Checked',onoff(UWPFLOW.opt.l.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig w', ...
            'Label','[-w] Write solution in IEEE CARD format', ...
            'Tag','wopt', ...
            'Checked',onoff(UWPFLOW.opt.w.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig W', ...
            'Label','[-W] Write solution in IEEE TAPE format', ...
            'Tag','wopt', ...
            'Checked',onoff(UWPFLOW.opt.W.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig y', ...
            'Label','[-y] Print left e-vector of smallest |e-value|', ...
            'Tag','yopt', ...
            'Checked',onoff(UWPFLOW.opt.y.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig Y', ...
            'Label','[-Y] Print right e-vector of smallest |e-value|', ...
            'Tag','Yopt', ...
            'Checked',onoff(UWPFLOW.opt.Y.status));

h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwfig Z', ...
            'Label','[-Z] Print normalized tangent vector', ...
            'Tag','Zopt', ...
            'Checked',onoff(UWPFLOW.opt.Z.status));

% Menu Preferences
h1 = uimenu('Parent',h0, ...
            'Label','Preferences', ...
            'Tag','MenuPref');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwpflow filename', ...
            'Label','Modify input/output file name', ...
            'Tag','OTV', ...
            'Accelerator','e');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_tviewer', ...
            'Label','Select Text Viewer', ...
            'Tag','tvopt', ...
            'Separator', 'on', ...
            'Accelerator','t');

% Menu Help
h1 = uimenu('Parent',h0, ...
            'Label','Help', ...
            'Tag','MenuHelp');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_uwpflow help', ...
            'Label','UWPFLOW help', ...
            'Accelerator','h', ...
            'Tag','UWHelp');
h2 = uimenu('Parent',h1, ...
            'Callback','web(''http://thunderbox.uwaterloo.ca/~claudio/software/pflow.html'');', ...
            'Label','UWPFLOW website', ...
            'Accelerator','w', ...
            'Tag','UWLink');

% Frame
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'ForegroundColor',Theme.color03, ...
               'Position',[0.0329 0.1399 0.9394 0.8405], ...
               'Style','frame', ...
               'Tag','Frame1');

% List Boxes
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig B', ...
               'HorizontalAlignment','left',  ...
               'Position',[x3 0.9199 dx 0.0339], ...
               'String','[-B] Bus for fixed voltage:', ...
               'Style','checkbox', ...
               'Tag','Check_B', ...
               'Value', UWPFLOW.opt.B.status);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.B.num = get(gcbo,''Value''); UWPFLOW.opt.B.num = UWPFLOW.opt.B.num(end); set(gcbo,''Value'',UWPFLOW.opt.B.num)', ...
               'Enable', onoff(UWPFLOW.opt.B.status), ...
               'FontName', Theme.font01, ...
               'ForegroundColor',Theme.color05,  ...
               'ListboxTop',UWPFLOW.opt.B.num, ...
               'Max', 100, ...
               'Position',[x3 0.7439 dx 0.1595], ...
               'String',PQbuses, ...
               'Style','listbox', ...
               'Tag','Edit_B', ...
               'Value',UWPFLOW.opt.B.num);

dyy = 0.015;

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig f', ...
               'HorizontalAlignment','left',  ...
               'Position',[x3 0.6835+dyy dx 0.0339], ...
               'String','[-f] Bus for SF, VSF and TG:', ...
               'Style','checkbox', ...
               'Tag','Check_f', ...
               'Value', UWPFLOW.opt.f.status);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.f.num = get(gcbo,''Value''); UWPFLOW.opt.f.num = UWPFLOW.opt.f.num(end); set(gcbo,''Value'',UWPFLOW.opt.f.num)', ...
               'Enable', onoff(UWPFLOW.opt.f.status), ...
               'FontName', Theme.font01, ...
               'ForegroundColor',Theme.color05,  ...
               'ListboxTop',UWPFLOW.opt.f.num, ...
               'Max', 100, ...
               'Position',[x3 0.5076+dyy dx 0.1595], ...
               'String',PQbuses, ...
               'Style','listbox', ...
               'Tag','Edit_f', ...
               'Value',UWPFLOW.opt.f.num);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig one', ...
               'HorizontalAlignment','left',  ...
               'Position',[x3 0.4471+2*dyy dx 0.0339], ...
               'String','[-1] Bus for test functions:', ...
               'Style','checkbox', ...
               'Tag','Check_one', ...
               'Value', UWPFLOW.opt.one.status);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.one.num = get(gcbo,''Value''); UWPFLOW.opt.one.num = UWPFLOW.opt.one.num(end); set(gcbo,''Value'',UWPFLOW.opt.one.num)', ...
               'Enable', onoff(UWPFLOW.opt.one.status), ...
               'FontName', Theme.font01, ...
               'ForegroundColor',Theme.color05,  ...
               'ListboxTop',UWPFLOW.opt.one.num, ...
               'Max', 100, ...
               'Position',[x3 0.2712+2*dyy dx 0.1595], ...
               'String',PQbuses, ...
               'Style','listbox', ...
               'Tag','Edit_one', ...
               'Value',UWPFLOW.opt.one.num);

% Popup Menus
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwpflow methods', ...
               'FontName', Theme.font01, ...
               'ForegroundColor',Theme.color05,  ...
               'HorizontalAlignment','left',  ...
               'Position',[x2 0.2712+2*dyy dx 0.0308], ...
               'String',methods, ...
               'Style','popupmenu', ...
               'Tag','PopupMenuMethod', ...
               'Value',UWPFLOW.method);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left',  ...
               'Position',[x2 0.305+2*dyy dx 0.0308], ...
               'String','Solver method:', ...
               'Style','text', ...
               'Tag','StaticText12');

h1 = uicontrol('Parent', h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', '', ...
               'FontName', Theme.font01, ...
               'HorizontalAlignment','left',  ...
               'Position',[x1 0.2712+2*dyy dx 0.0308], ...
               'String',output, ...
               'Style','popupmenu', ...
               'Tag','PopupUWFile', ...
               'Value',1);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[x1 0.305+2*dyy dx 0.0308], ...
               'String','UWPFLOW input/output file:', ...
               'Style','text', ...
               'Tag','StaticText11');

% Pushbuttons
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color03, ...
               'Callback','fm_uwpflow uwrun', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[x1 0.1576-2*dm dx 0.045+2*dm], ...
               'String','Run UWPFLOW', ...
               'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','close(gcf)', ...
               'Position',[x3 0.1576-2*dm dx 0.045+2*dm], ...
               'String','Close', ...
               'Tag','Pushbutton2');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_uwpflow view', ...
               'Position',[x2 0.1576-2*dm dx 0.045+2*dm], ...
               'String','View Input/Output File', ...
               'Tag','Pushbutton3');

% UWPFLOW Command Line
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','UWPFLOW.command = get(gcbo,''String'');', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x1 0.225 x3+dx-x1 0.0308+dm], ...
               'Style','edit', ...
               'String', UWPFLOW.command, ...
               'Tag','EditCom');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left',  ...
               'Position',[x1 0.225+0.0308+dm dx 0.0308], ...
               'String','Command Line:', ...
               'Style','text', ...
               'Tag','TextCom');

% Parameters (left column)
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.F.num = fval(gcbo,UWPFLOW.opt.F.num);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'Enable', onoff(UWPFLOW.opt.F.status), ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x1 0.8731 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.F.num), ...
               'Tag','Edit_F');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig F', ...
               'HorizontalAlignment','left', ...
               'Position',[x1 0.9199 dx 0.0339], ...
               'String','[-F] Stability/sparsity [0,1]', ...
               'Style','checkbox', ...
               'Tag','Check_F', ...
               'Value', UWPFLOW.opt.F.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.t.num = fval(gcbo,UWPFLOW.opt.t.num)', ...
               'Enable', onoff(UWPFLOW.opt.t.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt,  ...
               'Position',[x1 0.7764 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.t.num), ...
               'Tag','Edit_t');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'Callback', 'fm_uwfig t', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[x1 0.8232 dx 0.0339], ...
               'String','[-t] Iteration mismatch tol.', ...
               'Style','checkbox', ...
               'Tag','Check_t', ...
               'Value', UWPFLOW.opt.t.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','UWPFLOW.opt.o.num = fval(gcbo,UWPFLOW.opt.o.num);', ...
               'Enable', onoff(UWPFLOW.opt.o.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x1 0.6798 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.o.num), ...
               'Tag','Edit_o');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig o', ...
               'HorizontalAlignment','left', ...
               'Position',[x1 0.7266 dx 0.0339], ...
               'String','[-o] Limit control tol.', ...
               'Style','checkbox', ...
               'Tag','Check_o', ...
               'Value', UWPFLOW.opt.o.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.L.num = fval(gcbo,UWPFLOW.opt.L.num);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'Enable', onoff(UWPFLOW.opt.L.status), ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x1 0.5831 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.L.num), ...
               'Tag','Edit_L');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig L', ...
               'HorizontalAlignment','left', ...
               'Position',[x1 0.6299 dx 0.0339], ...
               'String','[-L] Initial loading factor', ...
               'Style','checkbox', ...
               'Tag','Check_L', ...
               'Value', UWPFLOW.opt.L.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.O.num = fval(gcbo,UWPFLOW.opt.O.num)', ...
               'Enable', onoff(UWPFLOW.opt.O.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt,  ...
               'Position',[x1 0.4865 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.O.num), ...
               'Tag','Edit_O');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'Callback', 'fm_uwfig O', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[x1 0.5333 dx 0.0339], ...
               'String','[-O] ac/dc TEF digits [6-10]', ...
               'Style','checkbox', ...
               'Tag','Check_O', ...
               'Value', UWPFLOW.opt.O.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','UWPFLOW.opt.S.num = fval(gcbo,UWPFLOW.opt.S.num);', ...
               'Enable', onoff(UWPFLOW.opt.S.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x1 0.3898 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.S.num), ...
               'Tag','Edit_S');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig S', ...
               'HorizontalAlignment','left', ...
               'Position',[x1 0.4366 dx 0.0339], ...
               'String','[-S] Max. loading factor [0,1]', ...
               'Style','checkbox', ...
               'Tag','Check_S', ...
               'Value', UWPFLOW.opt.S.status);

% Parameters (middle column)
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.k.num = fval(gcbo,UWPFLOW.opt.k.num);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'Enable', onoff(UWPFLOW.opt.k.status), ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x2 0.8731 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.k.num), ...
               'Tag','Edit_k');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig k', ...
               'HorizontalAlignment','left', ...
               'Position',[x2 0.9199 dx 0.0339], ...
               'String','[-k] Loading factor increment', ...
               'Style','checkbox', ...
               'Tag','Check_k', ...
               'Value', UWPFLOW.opt.k.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.u.num = fval(gcbo,UWPFLOW.opt.u.num)', ...
               'Enable', onoff(UWPFLOW.opt.u.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt,  ...
               'Position',[x2 0.7764 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.u.num), ...
               'Tag','Edit_u');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'Callback', 'fm_uwfig u', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[x2 0.8232 dx 0.0339], ...
               'String','[-u] Reduce eq. tol. [0,0.2]', ...
               'Style','checkbox', ...
               'Tag','Check_u', ...
               'Value', UWPFLOW.opt.u.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','UWPFLOW.opt.U.num = fval(gcbo,UWPFLOW.opt.U.num);', ...
               'Enable', onoff(UWPFLOW.opt.U.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x2 0.6798 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.U.num), ...
               'Tag','Edit_U');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig U', ...
               'HorizontalAlignment','left', ...
               'Position',[x2 0.7266 dx 0.0339], ...
               'String','[-U] Step # for sys. red. [2,100]', ...
               'Style','checkbox', ...
               'Tag','Check_U', ...
               'Value', UWPFLOW.opt.U.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.v.num = fval(gcbo,UWPFLOW.opt.v.num);', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'Enable', onoff(UWPFLOW.opt.v.status), ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x2 0.5831 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.v.num), ...
               'Tag','Edit_v');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig v', ...
               'HorizontalAlignment','left', ...
               'Position',[x2 0.6299 dx 0.0339], ...
               'String','[-v] PQ bus voltage magnitude', ...
               'Style','checkbox', ...
               'Tag','Check_v', ...
               'Value', UWPFLOW.opt.v.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback', 'UWPFLOW.opt.z.num = fval(gcbo,UWPFLOW.opt.z.num)', ...
               'Enable', onoff(UWPFLOW.opt.z.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x2 0.4865 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.z.num), ...
               'Tag','Edit_z');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'Callback', 'fm_uwfig z', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[x2 0.5333 dx 0.0339], ...
               'String','[-z] Max. # of CM steps', ...
               'Style','checkbox', ...
               'Tag','Check_z', ...
               'Value', UWPFLOW.opt.z.status);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','UWPFLOW.opt.two.num = fval(gcbo,UWPFLOW.opt.two.num);', ...
               'Enable', onoff(UWPFLOW.opt.two.status), ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[x2 0.3898 dx 0.0308+dm], ...
               'Style','edit', ...
               'String', num2str(UWPFLOW.opt.two.num), ...
               'Tag','Edit_two');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'fm_uwfig two', ...
               'HorizontalAlignment','left', ...
               'Position',[x2 0.4366 dx 0.0339], ...
               'String','[-2] Step # for dir. change', ...
               'Style','checkbox', ...
               'Tag','Check_two', ...
               'Value', UWPFLOW.opt.two.status);

% Banner
h1 = axes('Parent',h0, ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'Color',Theme.color04, ...
          'ColorOrder',Settings.color, ...
          'Layer','top', ...
          'Position',[0.0329 0.0169 0.8*0.1540 0.8*0.1371], ...
          'Tag','Axes1', ...
          'XColor',Theme.color02, ...
          'XLim',[0.5 100.5], ...
          'XLimMode','manual', ...
          'XTick',[], ...
          'YColor',Theme.color02, ...
          'YDir','reverse', ...
          'YLim',[0.5 100.5], ...
          'YLimMode','manual', ...
          'YTick',[], ...
          'ZColor',[0 0 0]);
if Settings.hostver < 8.04
  h2 = image('Parent',h1, ...
             'CData',fm_mat('logo_psat'), ...
             'Tag','Axes1Image1', ...
             'XData',[1 101], ...
             'YData',[1 101]);
else
  h2 = image('Parent',h1, ...
             'CData',flipud(fliplr(fm_mat('logo_psat'))), ...
             'Tag','Axes1Image1', ...
             'XData',[1 101], ...
             'YData',[1 101]);
end
h1 = axes('Parent',h0, ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'Color',Theme.color02, ...
          'ColorOrder',Settings.color, ...
          'Layer','top', ...
          'Position',[0.8491 0.0169 0.8*0.1540 0.8*0.1371], ...
          'Tag','Axes1', ...
          'XColor',Theme.color02, ...
          'XLim',[0.5 100.5], ...
          'XLimMode','manual', ...
          'XTick',[], ...
          'YColor',Theme.color02, ...
          'YDir','reverse', ...
          'YLim',[0.5 100.5], ...
          'YLimMode','manual', ...
          'YTick',[], ...
          'ZColor',[0 0 0]);
if Settings.hostver < 8.04
  h2 = image('Parent',h1, ...
             'CData',fm_mat('logo_uwpflow'), ...
             'Tag','Axes1Image1', ...
             'XData',[1 101], ...
             'YData',[1 101]);
else
  h2 = image('Parent',h1, ...
             'CData',flipud(fliplr(fm_mat('logo_uwpflow'))), ...
             'Tag','Axes1Image1', ...
             'XData',[1 101], ...
             'YData',[1 101]);
end
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'ForegroundColor', [0 0 1], ...
               'FontSize', 12, ...
               'FontName', 'Times', ...
               'FontWeight', 'bold', ...
               'FontAngle', 'italic',...
               'Position',[0.2915 0.0516 0.4221 0.0355], ...
               'String','PSAT-UWPFLOW Interface', ...
               'Style','text', ...
               'Tag','StaticText3');

if nargout > 0, fig = h0; end

% ---------------------------------------------------------
function output = onoff(input)

if input, output = 'on'; else, output = 'off'; end

% ---------------------------------------------------------
function checkon(field)

global UWPFLOW Fig

if isfield(UWPFLOW.opt,field)
  a = getfield(UWPFLOW.opt,field);
  a.status = ~a.status;
  UWPFLOW.opt = setfield(UWPFLOW.opt,field,a);

  try % strcmp(get(gcbo,'Style'),'checkbox')
    set(gcbo,'Value',a.status)
    hdl = findobj(Fig.uwpflow,'Tag',['Edit_',field]);
    set(hdl,'Enable',onoff(a.status))
  catch
    set(gcbo,'Checked',onoff(a.status))
  end

end
